package com.example.leo.mycharmingstudent.a0911;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.leo.mycharmingstudent.R;
import com.example.leo.mycharmingstudent.a0910.MyDynimicsReceiver;

public class LifeActivity2 extends AppCompatActivity {

    private static final String TAG = "B";
    public static final String LIFE_ACTIVITY_ACTION = "android.intent.my.LIFE";
    private MyDynimicsReceiver mDynimicsReceiver;
    private Button btnSendBroadcast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_life2);
        Log.e(TAG, "onCreate: ");

        findViews();
        initData();
        initEvent();
    }

    private void findViews() {
        btnSendBroadcast = findViewById(R.id.btn_send_broadcast_activity_life2);
    }

    private void initData() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(LIFE_ACTIVITY_ACTION);
        mDynimicsReceiver = new MyDynimicsReceiver();
        registerReceiver(mDynimicsReceiver,intentFilter);
    }

    private void initEvent() {
        btnSendBroadcast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //发送广播
                Intent intent = new Intent();
                intent.setAction(LIFE_ACTIVITY_ACTION);
                sendBroadcast(intent);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e(TAG, "onStart: ");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "onResume: ");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e(TAG, "onPause: ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e(TAG, "onStop: ");
        unregisterReceiver(mDynimicsReceiver);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e(TAG, "onRestart: ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy: ");
    }
}
